package com.pjs.tdd;

public abstract class AbstractTransaction implements Transaction{
	
	public enum TransactionType{
		negative,
		positive;
		public static TransactionType lookup(TransactionType transactionTypeToLookup){
			for (TransactionType transactionType : TransactionType.values()) {
				if(transactionType.equals(transactionTypeToLookup)){
					return transactionType;
				}
			}
			throw new TransactionException("Transaction type not found!");
		}
	}
	
	protected final TransactionType transactionType;
	protected final Money money;
	private final TransactionReference transactionReference=new SimpleTransactionReference();
	
	public AbstractTransaction(final TransactionType transactionType, final Money amount){
		this.transactionType=transactionType;
		this.money=amount;
		assertMoney();
	}

	private void assertMoney() {
		if(this.money.amount()<0){
			throw new TransactionException("Money has an amount less than zero");
		}
	}

	@Override
	public TransactionType type() {
		return this.transactionType;
	}
	
	public TransactionReference transactionReference() {
		return this.transactionReference;
	}
	
	@Override
	public Money transactionAmount(){
		return this.money;
	}

	public abstract Money apply(Money money) ;
	
}
